﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System;
using System.IO;

public class BrushWorldPath : EditorWindow
{
    bool hasPath = false;
    bool beginBrush = false;
    
    List<Vector3> onePath = new List<Vector3>();
    int[,] cityLinks = new int[200, 200];

    private CatchMouse com = null;
    private GameObject catchMouse = null;
    private GameObject prefab = null;

    [MenuItem("Window/My Window")]
    static void Init()
    {
        BrushWorldPath win = EditorWindow.GetWindow(typeof(BrushWorldPath)) as BrushWorldPath;
    }

    void OnGUI()
    {
        GetCatchMouse();

        GUILayout.Label("刷世界地图路线", EditorStyles.boldLabel);


        GUILayout.BeginHorizontal();
        Transform newFromCity = (Transform)EditorGUILayout.ObjectField("出发城市", com.FromCity, typeof(Transform), true, GUILayout.MaxWidth(position.width / 2));
        Transform newToCity = (Transform)EditorGUILayout.ObjectField("目的城市", com.ToCity, typeof(Transform), true, GUILayout.MaxWidth(position.width / 2));
        GUILayout.EndHorizontal();

        if (com.FromCity != newFromCity)
        {
            //Debug.LogFormat("old:{0},new:{1}", FromCity.name, newFromCity.name);
            com.FromCity = newFromCity;
            com.fromCityID = GetCityID(com.FromCity);
            hasPath =  com.CheckHasPath();
        }

        if (com.ToCity != newToCity)
        {
            com.ToCity = newToCity;
            com.toCityID = GetCityID(com.ToCity);
            hasPath =  com.CheckHasPath();
        }

        string hasPathStr = hasPath ? "有路线" : "无路线";
        GUIStyle style = new GUIStyle();
        style.normal.textColor = hasPath ? Color.red : Color.white;

        GUILayout.BeginHorizontal();
        GUILayout.Label("所选城池路线状态:", GUILayout.MaxWidth(position.width / 2));
        GUILayout.Label(hasPathStr, style);
        GUILayout.EndHorizontal();

        com.sampleDis = EditorGUILayout.FloatField("采样距离:", com.sampleDis);

        GUILayout.BeginHorizontal();
        com.CityRoot = (Transform)EditorGUILayout.ObjectField("城池根节点", com.CityRoot, typeof(Transform), true, GUILayout.MaxWidth(position.width * 3 / 4));
        bool nameCity = GUILayout.Button("挂上名字版", GUILayout.MaxWidth(position.width / 4));
        GUILayout.EndHorizontal();
        if (nameCity)
        {
            if (com.CityRoot != null)
            {
                MountCityName(com.CityRoot);
            }
        }

        bool draw = GUILayout.Button("绘制路线", GUILayout.MaxWidth(position.width));
        if (draw)
        {
            com.DrawPath();
        }

        bool saveData = GUILayout.Button("保存路线数据到预制体", GUILayout.MaxWidth(position.width));
        if (saveData)
        {
            com.SaveData(prefab);
        }

        bool clear = GUILayout.Button("清除当前路线", GUILayout.MaxWidth(position.width));
        if (clear)
        {
            if (EditorUtility.DisplayDialog("清除当前路线", "是否清除当前所选城池路线", "ok", "cancel"))
            {
                com.ClearPath();
            }
        }

        bool clearAll = GUILayout.Button("清除所有路线", GUILayout.MaxWidth(position.width));
        if (clearAll)
        {
            if (EditorUtility.DisplayDialog("清除所有路线", "是否清除所有路线", "ok", "cancel"))
            {
                com.ClearAllPath();
            }
        }

        bool save = GUILayout.Button("保存路线图", GUILayout.MaxWidth(position.width), GUILayout.MaxHeight(50f));
        if (save)
        {
            com.GenerateCityPath();
        }

        EditorUtility.SetDirty(com);
    }



    private int GetCityID(Transform city)
    {
        if (city == null)
        {
            return 0;
        }

        string cityName = city.name;
        if (cityName.StartsWith(com.cityNamePrefix))
        {
            int ret = 0;
            bool parse = Int32.TryParse(cityName.Substring(com.cityNamePrefix.Length), out ret);
            if (parse)
            {
                return ret;
            }
            return 0;
        }

        Debug.LogErrorFormat("城市节点名字格式不对,应该为{0}123这样的格式", com.cityNamePrefix);
        return 0;
    }
    
 
    private GameObject GetPathSpotRoot()
    {
        GameObject spawn = GameObject.FindGameObjectWithTag("Respawn");
        if (spawn == null)
        {
            spawn = new GameObject("path_spot_root");
            spawn.tag = "Respawn";
        }
        return spawn;
    }
    
    private void MountCityName(Transform cityroot)
    {
        GameObject nameRoot = GetCityRoot();
        if (nameRoot != null)
        {
            DestroyImmediate(nameRoot);
        }
        nameRoot = GetCityRoot();

        foreach (var item in cityroot)
        {
            Transform city = item as Transform;
            GameObject prefab = Resources.Load("just_for_path/prefab_city_name") as GameObject;
            GameObject obj = Instantiate(prefab);
            obj.transform.SetParent(nameRoot.transform);
            obj.transform.position = city.position;
            obj.GetComponent<TextMesh>().text = city.name;
        }
    }


 

    private GameObject GetCityRoot()
    {
        GameObject obj = GameObject.Find("city_name_root");
        if (obj == null)
        {
            obj = new GameObject("city_name_root");
        }

        return obj;
    }

    private void GetCatchMouse()
    {
        
        if(catchMouse == null)
        {
            catchMouse = GameObject.Find("prefab_catch_mouse");
        }

        if (catchMouse == null)
        {
            string path = "Assets/Resources/just_for_path/prefab_catch_mouse.prefab";
            prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
            catchMouse = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
            catchMouse.name = "prefab_catch_mouse";
        }

        if(com == null)
        {
            com = catchMouse.GetComponent<CatchMouse>();
        }

        if(com == null)
        {
            com = catchMouse.AddComponent<CatchMouse>();
        }
    }
}
